import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:ts_app_dart/widget/dialog_widget.dart';
import 'package:ts_app_dart/widget/popup_window.dart';
import 'package:ts_app_dart/widget/animation_widget.dart';

Future<T> showPopupWindow<T>({
  BuildContext context,
  RenderBox anchor,
  Widget child,
  Offset offset,
  String semanticLabel,
  bool isShowBg = false,
}) {
  switch (defaultTargetPlatform) {
    case TargetPlatform.iOS:
    case TargetPlatform.macOS:
      break;
    case TargetPlatform.android:
    case TargetPlatform.fuchsia:
    case TargetPlatform.linux:
    case TargetPlatform.windows:
      semanticLabel ??= MaterialLocalizations.of(context).popupMenuLabel;
  }
  final RenderBox overlay =
      Overlay.of(context).context.findRenderObject() as RenderBox;

  // 默认位置锚点下方
  final Offset defaultOffset = Offset(0, anchor.size.height);

  if (offset == null) {
    offset = defaultOffset;
  } else {
    offset = offset + defaultOffset;
  }
  // 获得控件左下方的坐标
  final a = anchor.localToGlobal(offset, ancestor: overlay);
  // 获得控件右下方的坐标
  final b =
      anchor.localToGlobal(anchor.size.bottomLeft(offset), ancestor: overlay);

  final RelativeRect position = RelativeRect.fromRect(
    Rect.fromPoints(a, b),
    Offset.zero & overlay.size,
  );

  return Navigator.push(
    context,
    PopupWinRoute(
      child,
      position: position,
      semanticLabel: semanticLabel,
      isShowBg: isShowBg,
    ),
  );
}

class DialogPage extends StatefulWidget {
  const DialogPage({Key key}) : super(key: key);

  @override
  _DialogPageState createState() => _DialogPageState();
}

class _DialogPageState extends State<DialogPage> with TickerProviderStateMixin {
  final GlobalKey _buttonKey = GlobalKey();
  final message = """
    如果发出声音是危险的，那就保持沉默;
    如果自觉无力发光，那就别去照亮别人。
    但是——不要习惯了黑暗就为黑暗辩护;
    不要为自己的苟且而得意洋洋;
    不要嘲讽那些比自己更勇敢、更有热量的人们。
    我们可以卑微如尘土，不可扭曲如蛆虫。
    ——曼德拉《漫漫人生路》
    """;

  @override
  void initState() {
    super.initState();
  }

  void onPressed() {
    BuildContext buttonCtx = _buttonKey.currentContext;
    final RenderBox button = buttonCtx.findRenderObject();

    print('button.size, ${button.size}');

    showPopupWindow(
      context: context,
      anchor: button,
      isShowBg: true,
      offset: Offset(button.size.width, -10.0),
      child: AnimationWidget(),
    );

    // showCustomAlert();
  }

  void showCustomAlert() {
    bool isCustomVal = false;
    if (isCustomVal == true) {
      Navigator.push(
        context,
        NPopupRoute(
          alignment: Alignment.topCenter,
          onClick: () {
            //点击空白处
            Navigator.of(context).pop();
          },
          child: NAlertDialog(
            header: Text(
              'Alert',
              style: TextStyle(fontWeight: FontWeight.w500),
            ),
            content: Text(
              message,
              style: TextStyle(fontWeight: FontWeight.w300),
            ),
            cancelButton: TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text("取消"),
            ),
            confirmButton: TextButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              child: Text("确定"),
            ),
          ),
        ),
      );
    } else {
      Navigator.push(
        context,
        NPopupRoute(
          alignment: Alignment.topCenter,
          onClick: () {
            Navigator.of(context).pop();
          },
          // child: Container(
          //   child: Text('This is a example'),
          // ),
          child: GestureDetector(
            onTap: () {},
            child: Container(
              width: 320.0,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular((10.0)), // 圆角度
              ),
              child: Padding(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Padding(
                      padding: EdgeInsets.all(6),
                      child: Text(
                        'This is a Title',
                        style: TextStyle(fontWeight: FontWeight.w600),
                      ),
                    ),
                    Padding(
                      padding: EdgeInsets.all(6),
                      child: Text(
                        message,
                        style: TextStyle(fontWeight: FontWeight.w300),
                      ),
                    ),
                  ],
                ),
                padding: EdgeInsets.all(12),
              ),
            ),
          ),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DialogPage'),
      ),
      body: Container(
        child: Center(
          child: TextButton(
              key: _buttonKey, onPressed: onPressed, child: Text('TextButton')),
        ),
      ),
    );
  }
}
